﻿using B.MESUI.Login.A_Public.Helpers;
using B.MESUI.Login.BasicData_UI.BasicData;
using B.MESUI.Login.BasicData_UI.BasicData.Select;
using B.MESUI.Login.Properties;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Standard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace B.MESUI.Login.BasicData_UI.Supplier
{
    
    public partial class UpdateSupplierForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void EditSupplierEvent();
        public event EditSupplierEvent Supplier_Event;
        public UpdateSupplierDTO SupplierDTO;
        public UpdateSupplierForm(UpdateSupplierDTO supplierDTO)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            SupplierDTO = supplierDTO;
            LoadCustomerData();
        }
        /// <summary>
        /// 加载客户数据到表单
        /// </summary>
        private void LoadCustomerData()
        {
            if (SupplierDTO == null)
                return;
            textEdit1.Text = SupplierDTO.ID;
            textEdit2.Text = SupplierDTO.supplieR_NAME;
            textEdit3.Text = SupplierDTO.supplieR_PHONE;
            textEdit4.Text = SupplierDTO.crediT_CODE;
            textEdit5.Text = SupplierDTO.supplieR_CODE;
            textEdit6.Text = SupplierDTO.supplieR_ABBREVIATION;
            textEdit7.Text = SupplierDTO.supplieR_ADDRESS;
            textEdit8.Text = SupplierDTO.supplieR_NODE;

        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSupplierDTO _supplierDto = new UpdateSupplierDTO();
                // 收集表单数据
                _supplierDto.ID = textEdit1.Text.Trim();
                _supplierDto.supplieR_NAME = textEdit2.Text.Trim();
                _supplierDto.supplieR_PHONE = textEdit3.Text.Trim();
                _supplierDto.crediT_CODE = textEdit4.Text.Trim();
                _supplierDto.supplieR_CODE = textEdit5.Text.Trim();
                _supplierDto.supplieR_ABBREVIATION = textEdit6.Text.Trim();
                _supplierDto.supplieR_ADDRESS = textEdit7.Text.Trim();
                _supplierDto.supplieR_NODE = textEdit8.Text.Trim();
              

                // 调用API
                string url = Settings.Default.BaseUrl_SLY + MethodConst.UpdateSupplier;
                string result = await HttpClientHelper.PutAsync(url, _supplierDto);

                // 解析API返回结果
                var apiResult = JsonConvert.DeserializeObject<ApiResult<object>>(result);

                if (apiResult != null && apiResult.Code == 200)
                {
                    XtraMessageBox.Show("客户信息修改成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    // 触发事件通知父窗体刷新数据
                    Supplier_Event?.Invoke();

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show($"修改失败：{apiResult?.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

       
    }
}